package com.lagou.servlet;

import com.lagou.pojo.Result;
import com.lagou.service.AccountService;
import com.lagou.service.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @Authther: mrwu
 * @date:2021/3/12-03
 * @Description:com.lagou.servlet
 */
@WebServlet(urlPatterns = "/transferServlet")
public class TransferServlet extends HttpServlet {
    //初始化service对象
    private AccountService accountService=new AccountServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //将get指向post处理
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符编码
        req.setCharacterEncoding("UTF-8");
        //获取请求参数
        String fromcardNo = req.getParameter("fromCardNo");
        String tocardNo = req.getParameter("tocardNo");
        String moneys = req.getParameter("money");
        int money = Integer.parseInt(moneys);
        Result result = new Result();

        try {
            accountService.transfer(fromcardNo, tocardNo, money);
            result.setStatus("200");
            result.setMassage("成功");
            resp.getWriter().write(result.toString());
            resp.getWriter().flush();
        }catch (Exception e){
            result.setStatus("201");
            result.setMassage(e.toString());
            resp.getWriter().write(result.toString());
            resp.getWriter().flush();
        }

    }

}
